package edu.pku.sei.transformation.editor.model.execution;

import javax.lang.model.type.PrimitiveType;

import edu.pku.sei.modeler.model.core.MetaDataType;
import edu.pku.sei.modeler.model.core.MetaModelElement;
import edu.pku.sei.transformation.editor.model.ModelType;
import edu.pku.sei.transformation.editor.model.TypedModel;

public class ObjectFlow extends Edge {
	public INode getSource() {
		return (INode) sourceElement;
	}

	public void setSource(INode source) {
		if (source instanceof MetaModelElement)
			this.sourceElement = (MetaModelElement) source;
	}

	public INode getTarget() {
		return (INode) targetElement;
	}

	public void setTarget(INode target) {
		if (target instanceof MetaModelElement)
			this.targetElement = (MetaModelElement) target;
	}

	public boolean checkType() {
		Object sourceType = null;
		Object targetType = null;
		if (sourceElement instanceof Model) {
			sourceType = ((Model)sourceElement).getModelType();
		} else if (sourceElement instanceof Pin) {
			Pin p = (Pin) sourceElement;
			if (p.getFormalParameter() != null) {
				sourceType = p.getFormalParameter().getType();
			}
		} else if (sourceElement instanceof TypedModel) {
			sourceType = ((TypedModel) sourceElement).getType();
		}
		
		if (targetElement instanceof Model) {
			targetType = ((Model)targetElement).getModelType();
		} else if (targetElement instanceof Pin) {
			Pin p = (Pin) targetElement;
			if (p.getFormalParameter() != null) {
				targetType = p.getFormalParameter().getType();
			}
		} else if (targetElement instanceof TypedModel) {
			targetType = ((TypedModel) targetElement).getType();
		}
		
		//at least one side didn't bind type
		if (sourceType == null || targetType == null) {
			return true;
		} else if (sourceType instanceof ModelType && targetType instanceof ModelType) {
			return ((ModelType)targetType).isCompatible((ModelType) sourceType);
		} else if (sourceType instanceof MetaDataType && targetType instanceof MetaDataType) {
			if (sourceType == targetType)
				return true;
			else
				return false;
		} else {
			return false;
		}
		
		
		
	}
}
